{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import lightgbm as lgb\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import StratifiedKFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_path = '../../../contest/train/'\n",
    "stage_path = '../../../contest/A榜/'\n",
    "stage = 'A'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train = pd.read_csv('../../../contest/train/DZ_TARGET_TRAIN.csv')\n",
    "df_test = pd.read_csv('../../../contest/A榜/DZ_TARGET_TESTA.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train_user = pd.DataFrame({'CUST_NO':df_train.CUST_NO})\n",
    "df_test_user = pd.DataFrame({'CUST_NO':df_test.cust_no})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 自然属性信息表（DZ_NATURE）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = pd.read_csv(os.path.join(train_path,'DZ_NATURE.csv'))\n",
    "tmp_test = pd.read_csv(os.path.join(stage_path,f'DZ_NATURE_{stage}.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['DATA_DAT', 'CUST_NO', 'NTRL_CUST_SEX_CD', 'NTRL_CUST_AGE',\n",
       "       'NTRL_RANK_CD'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fe_nature(df):\n",
    "    rank = {'A':0,'B':1,'C':2,'D':3,'E':4}\n",
    "    sex = {'A':0,'B':1,'C':2}\n",
    "    df = df.drop('DATA_DAT',axis=1)\n",
    "    df['NTRL_CUST_SEX_CD'] = df['NTRL_CUST_SEX_CD'].map(sex)\n",
    "    df['NTRL_RANK_CD'] = df['NTRL_RANK_CD'].map(rank)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = fe_nature(tmp_train)\n",
    "tmp_test = fe_nature(tmp_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train = df_train_user.merge(tmp_train,on='CUST_NO',how='left')\n",
    "tmp_test = df_test_user.merge(tmp_test,on='CUST_NO',how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CUST_NO</th>\n",
       "      <th>NTRL_CUST_SEX_CD</th>\n",
       "      <th>NTRL_CUST_AGE</th>\n",
       "      <th>NTRL_RANK_CD</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>235e4e193124d8c55095cf3f0f0d8f35</td>\n",
       "      <td>0</td>\n",
       "      <td>47.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>f1b5ca32a8f7ef5430f5775c00ff3f60</td>\n",
       "      <td>1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>51be6f380b408edeb7779b76e016dcd3</td>\n",
       "      <td>1</td>\n",
       "      <td>54.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ccd7e33ccbe7e9dd4246a2959f666c0a</td>\n",
       "      <td>0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>069f48f51bf6be5bcbdc9af52bb20970</td>\n",
       "      <td>0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                            CUST_NO  NTRL_CUST_SEX_CD  NTRL_CUST_AGE  \\\n",
       "0  235e4e193124d8c55095cf3f0f0d8f35                 0           47.0   \n",
       "1  f1b5ca32a8f7ef5430f5775c00ff3f60                 1           30.0   \n",
       "2  51be6f380b408edeb7779b76e016dcd3                 1           54.0   \n",
       "3  ccd7e33ccbe7e9dd4246a2959f666c0a                 0           51.0   \n",
       "4  069f48f51bf6be5bcbdc9af52bb20970                 0           48.0   \n",
       "\n",
       "   NTRL_RANK_CD  \n",
       "0             2  \n",
       "1             0  \n",
       "2             2  \n",
       "3             2  \n",
       "4             2  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#pd.set_option('display.max_columns',None)\n",
    "tmp_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_train.to_csv('../fea/train_cust.csv',index=False)\n",
    "tmp_test.to_csv('../fea/test_cust.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
